package br.com.manutencaocarro.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import br.com.manutencaocarro.modelo.Peca;

public class RelatorioDAO 
{
	private DatabaseHelper helper;

	public RelatorioDAO( Context context ) {
		helper = new DatabaseHelper(context);
	}

	@SuppressLint({ "SimpleDateFormat", "DefaultLocale" })
	public ArrayList<Peca> geraRelatorio( Date inicialDate, Date finalDate, Integer idCarro )
	{
		HashMap<String, String> months = new HashMap<>();
		months.put( "JAN", "1" );
		months.put( "FEV", "2" );
		months.put( "FEB", "2" );
		months.put( "MAR", "3" );
		months.put( "ABR", "4" );
		months.put( "APR", "4" );
		months.put( "MAI", "5" );
		months.put( "MAY", "5" );
		months.put( "JUN", "6" );
		months.put( "JUL", "7" );
		months.put( "AGO", "8" );
		months.put( "AUG", "8" );
		months.put( "SET", "9" );
		months.put( "SEP", "9" );
		months.put( "OUT", "10" );
		months.put( "OCT", "10" );
		months.put( "NOV", "11" );
		months.put( "DEZ", "12" );
		months.put( "DEC", "12" );
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
		
		String[] args = { idCarro.toString(), simpleDateFormat.format(inicialDate), simpleDateFormat.format(finalDate) };
		Cursor cursor = helper.getWritableDatabase().query( DatabaseHelper.Peca.TABELA, DatabaseHelper.Peca.COLUNAS, 
															DatabaseHelper.Peca.ID_CARRO + " = ? AND (" + 
															DatabaseHelper.Peca.DATA_COMPRA + " BETWEEN ? AND ? )", args, 
															null, null, "" );

		ArrayList<Peca> pecas = new ArrayList<>();

		String date;
		String day;
		String month;
		String year;
		GregorianCalendar calendar;
		while( cursor.moveToNext() )
		{
			Peca peca = new Peca();
			peca.setId( cursor.getInt(0) );
			peca.setNomePeca( cursor.getString(1) );
			peca.setQtdPecas( cursor.getLong(2) );
			peca.setUnidade( cursor.getInt(3) );
			peca.setValorPeca( cursor.getFloat(4) );
			date = cursor.getString(5);
			
			if( date.matches("\\d{4}-\\d{2}-\\d{2}") )
			{
				day = date.split( "-" )[2];
				month = date.split( "-" )[1];
				year = date.split( "-" )[0];
			} else{
				day = date.split( " " )[2];
				month = date.split( " " )[1];
				year = date.split( " " )[5];
			}
		
			if( !month.equals(null) && !month.matches("\\d{2}") )
				month = months.get(month.toUpperCase());
			
			calendar = new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
			calendar.add( GregorianCalendar.MONTH, -1 );
			peca.setDataCompra( calendar.getTime() );
			peca.setIdCarro( cursor.getInt(6) );
			peca.setIdLoja( cursor.getInt(7) );

			pecas.add(peca);
		}

		return pecas;
	}
	
	public void close(){
		helper.close();
	}
}